package com.lm.paymentadmin.exception;


import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.lm.paymentadmin.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.util.List;

/**
 * 全局异常
 * @author liu2dog
 */
@RestControllerAdvice
@Slf4j
public class GlobalExcepitonHandler {


    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        e.printStackTrace();
        log.info(e.getMessage());
        return Result.fail(e.getMessage());
    }

    /**
     * Sentinel 异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(BlockException.class)
    public Object BlockExceptionHandler(BlockException e){
        log.info("BlockExceptionHandler BlockException====, rule: {}", e.getRule());
        Result r = null;
        if (e instanceof FlowException) {
            r = Result.fail(100, "接口限流了",null);

        } else if (e instanceof DegradeException) {
            r = Result.fail(101, "服务降级了",null);

        } else if (e instanceof ParamFlowException) {
            r = Result.fail(102, "热点参数限流了",null);

        } else if (e instanceof SystemBlockException) {
            r = Result.fail(103, "触发系统保护规则了",null);

        } else if (e instanceof AuthorityException) {
            r = Result.fail(104, "授权规则不通过",null);
        }else {
            r = Result.fail("系统错误");
        }
        return r;
    }

    /**
     * 校验错误拦截处理
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validationBodyException(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            errors.forEach(p -> {
                FieldError fieldError = (FieldError)p;
            });
            return Result.fail(result.getFieldError() == null ? "请求参数有误" : result.getFieldError().getDefaultMessage());
        }
        return  Result.fail("系统错误");
    }
}
